package com.time.hellotime.components.discovery;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.time.hellotime.R;
import com.time.hellotime.base.BaseViewHolder;
import com.time.hellotime.base.BaseXRecAdapter;
import com.time.hellotime.servermodel.discovery.Discovery;
import com.time.hellotime.utils.ScreenUtils;
import com.time.hellotime.utils.TimeUtils;
import com.time.hellotime.webserver.helper.picasso.PicassoImageLoader;

import java.util.List;

/**
 * 我的志愿 - 征集中
 * Created by Bing on 2017/11/9.
 * E-mail is tx16bing@163.com
 */

public class CollAdapter_ extends BaseXRecAdapter<Discovery> {

    private String mNowTime;

    public void setNowTime(String nowTime) {
        this.mNowTime = nowTime;
    }

    public CollAdapter_(Context mContext, List<Discovery> list, int viewTypeCount) {
        super(mContext, list, viewTypeCount);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View convertView = inflater.inflate(R.layout.item_vol_coll_list, parent, false);
        VolViewHolder publishViewHolder = new VolViewHolder(convertView);
        convertView.setTag(publishViewHolder);
        return publishViewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position, boolean isItem) {
        final Discovery discovery = getItem(position);
        VolViewHolder volHolder = (VolViewHolder) holder;
        if (!TextUtils.isEmpty(discovery.getCoverPicUrl())) {
            int width = ScreenUtils.dip2px(92);
            int height = ScreenUtils.dip2px(68);
            PicassoImageLoader.loadImageViewSize(mContext,
                    discovery.getCoverPicUrl(),
                    width,
                    height,
                    R.drawable.iv_default_92_68,
                    R.drawable.iv_default_92_68,
                    toString(), volHolder.ivPic);
        }
        volHolder.tvLocation.setText(discovery.getCity());
        volHolder.tvTitle.setText(discovery.getTitle());

        boolean canGo = TimeUtils.compareTime(mNowTime, discovery.getStartDate());
        if (canGo) {
            long[] distanceTimes = TimeUtils.getDistanceTimes(mNowTime, discovery.getStartDate());
            if (distanceTimes.length > 0) {
                long day = distanceTimes[0];
                volHolder.tvTime.setText(new StringBuilder().append("距离开始还有").append(day).append("天"));
            } else {
                volHolder.tvTime.setText(new StringBuilder().append("距离开始还有").append("0").append("天"));
            }
        } else {
            volHolder.tvTime.setText(new StringBuilder().append("距离开始还有").append("0").append("天"));
        }

        volHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemClickListener){
                    if (TextUtils.isEmpty(discovery.getId())){
                        return;
                    }else {
                        onItemClickListener.onItemClick(v, position, discovery, getViewType());
                    }
                }
            }
        });

        volHolder.llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemClickListener){
                    if (TextUtils.isEmpty(discovery.getId())){
                        return;
                    }else {
                        onItemClickListener.onItemClick(v, position, discovery, getViewType());
                    }
                }
            }
        });

    }

    class VolViewHolder extends BaseViewHolder {

        public LinearLayout llParent;
        public ImageView ivPic;
        public TextView tvLocation;
        public TextView tvTitle;
        public TextView tvTime;
        public ImageView ivDelete;
        public View divider;

        public VolViewHolder(View itemView) {
            super(itemView);
            llParent = getView(R.id.ll_vol_coll_item_parent);
            ivPic = getView(R.id.iv_vol_coll_pic);
            tvLocation = getView(R.id.tv_vol_coll_location);
            tvTitle = getView(R.id.tv_vol_coll_title);
            tvTime = getView(R.id.tv_vol_time);
            ivDelete = getView(R.id.iv_exe_delete);
            divider = getView(R.id.v_divider);
        }
    }
}
